/** \file uart.h
 **
 ** uart file
 **
 ** History:
 **   - 2013-11-20  1.0  Mark  First Version 
 *************************************************************************/

#ifndef __UART_H__
#define __UART_H__

/*****************************************************************************/
/* Include files                                                             */
/*****************************************************************************/
#include "mcu.h"
#include "base_types_l3.h"
//#include <string.h>
//#include "includes.h"
/*****************************************************************************/
/* Global pre-processor symbols/macros ('#define')                           */
/*****************************************************************************/
#define USE_PRINTF      0
#define UART_USE_L3 	0

#if (USE_PRINTF == 1)
    #include <stdio.h>
#endif
#define  HEAP_SIZE     16

#ifndef CLOCK_FREQ
    #ifdef __HCLK
        #define CLOCK_FREQ __HCLK>>APBC0_PSR_Val
    #else
        #define CLOCK_FREQ 40000000UL
    #endif
#endif

#if (UART_USE_L3 == 0)
//    #define UART_BAUDRATE(x)  (uint16_t)((CLOCK_FREQ+x/2)/(uint32_t)x - 1)
    #define UART_BAUDRATE(x)  (uint16_t)(40000000UL/(uint32_t)x - 1)
#else
    #define UART_BAUDRATE(x)  x
#endif

#if UART_USE_L3 == 1
    #include "mfs.h"
#endif

/*****************************************************************************/
/* Global type definitions ('typedef')                                       */
/*****************************************************************************/

/*****************************************************************************/
/* Global variable declarations ('extern', definition in C source)           */
/*****************************************************************************/
extern boolean_t Mfs0RxFull;
extern boolean_t Mfs1RxFull;
extern boolean_t Mfs3RxFull;
extern boolean_t Mfs6RxFull;
extern boolean_t Mfs7RxFull;
extern char line_buffer[255];
/*****************************************************************************/
/* Global function prototypes ('extern', definition in C source)             */
/*****************************************************************************/

boolean_t Uart_Init(uint8_t u8Uart, uint32_t Baudrate);
boolean_t Uart_Deinit(uint8_t u8Uart);
void Uart_Putch(uint8_t u8Uart, char_t c);
void putch(char_t c);
char_t Uart_Getch(uint8_t u8Uart);
char_t getch();
void Uart_Puts(uint8_t u8Uart, char_t* String);
int puts(const char_t* String);
void puthex(uint32_t n, uint8_t digits);
void putdec(uint32_t x);
unsigned long ASCIItobin(uint8_t k);
void receive_line(void);
int receive_line_echo(int *cnt);
int scan_line(char_t *str) ;
unsigned long Inputhex(uint8_t digits);
char upcase(char_t k);

void UartRxIrqHandler(uint8_t ch);
void UartTxIrqHandler(uint8_t ch);
/* Low-Level functions */
#if (USE_PRINTF == 1)
    int __write(int , char *, unsigned int);
    int __close(int);
    int __read(int , char *, unsigned int);
#endif /* (USE_PRINTF == 1) */

#endif /* __UART_H__ */
